﻿namespace RpcExtend.Model
{
    public enum IpType
    {
        Ip4=0,
        Ip6=1,
    }
    public class IpBlack : IEquatable<IpBlack>
    {
        /// <summary>
        /// ID
        /// </summary>
        public int Id
        {
            get;
            set;
        }
        public IpType IpType
        {
            get;
            set;
        }
        public string Ip6
        {
            get;
            set;
        }
        /// <summary>
        /// 开始IP
        /// </summary>
        public long Ip
        {
            get;
            set;
        }
        /// <summary>
        /// 结束Ip
        /// </summary>
        public long? EndIp
        {
            get;
            set;
        }
        /// <summary>
        /// 是否删除
        /// </summary>
        public bool IsDrop
        {
            get;
            set;
        }
    
        private int _Code = -1;
        public override int GetHashCode()
        {
            if (_Code == -1)
            {
                _Code = string.Concat(Ip, "_", EndIp.GetValueOrDefault()).GetHashCode();
            }
            return _Code;
        }
        public override bool Equals(object obj)
        {
            if (obj is IpBlack i)
            {
                return i.Ip == Ip && !i.EndIp.HasValue && !EndIp.HasValue || i.Ip == Ip && i.EndIp.HasValue && EndIp.HasValue && i.EndIp.Value == EndIp.Value;
            }
            return false;
        }

        public bool Equals(IpBlack other)
        {
            if (other == null)
            {
                return false;
            }
            return other.Ip == Ip && !other.EndIp.HasValue && !EndIp.HasValue || other.Ip == Ip && other.EndIp.HasValue && EndIp.HasValue && other.EndIp.Value == EndIp.Value;
        }
    }
}
